
var app = getApp();
var ApiUrl = app.globalData.ApiUrl;
var mph = require("../../utils/util.js");
var app = getApp();
Page({
  data: {
    winHeight: "",//窗口高度
    currentTab: 0, //预设当前项的值
    scrollLeft: 0, //tab标题的滚动条位置
    expertList: [     {
                      id:0,
                      u_name: '',
                      text:'全部'
                    }, {
                      id:1,
                      u_name: 'state_new',
                      text:'待支付'
                    },{
                      id: 2,
                      u_name: 'daipeisong',
                      text: '待配送'
                    }, {
                      id: 3,
                      u_name: 'state_send',
                      text: '配送中'
                    }, {
                      id: 4,
                      u_name: 'state_noeval',
                      text: '待评价'
                    }
    ],
    contentList:''
  },
  go_order_detail(e){

    let that = this;
    console.log(e,e.currentTarget.dataset);
    let order_id = e.currentTarget.dataset.order_id;
    let pay_sn = e.currentTarget.dataset.pay_sn;

    // for (let i = 0; i < that.data.contentList.length;i++){
    //   if (that.data.contentList[i].order_list[0].order_id === order_id){
    //     wx.setStorageSync('order_content', that.data.contentList[i].order_list[0]);
    //   }
    // }

    wx.redirectTo({
      url: '/pages/order_detail/order_detail?order_id=' + order_id + '&pay_sn=' + pay_sn
    })
  },
  go_after_sales_type(e) {
    let order_id = e.currentTarget.dataset.order_id;
    wx.redirectTo({
      url: '/pages/commodity_after_sale/commodity_after_sale?order_id=' + order_id 
    })
  },
  go_pingjia(e) {
    let order_id = e.currentTarget.dataset.order_id;
    wx.redirectTo({
      url: '/pages/pingjia/pingjia?order_id=' + order_id
    })
  },
  go_shopping_cart(e){//跳转购物车
    let order_id = e.currentTarget.dataset.order_id;
    let arr = [];
    let arrS = [];
    let strS = '';
    if (this.data.contentList.length > 0) {
      for (let i = 0; i < this.data.contentList.length;i++) {
        if (this.data.contentList[i].order_list[0].order_id === order_id){
          //goods_id1, num1| goods_id2, num2
          arr = this.data.contentList[i].order_list[0].extend_order_goods;
        }
      }
    }

    for (let i = 0; i < arr.length;i++){
      let str = arr[i].goods_id + ',' + arr[i].goods_num;
      arrS.push(str);
    }

    strS = arrS.join('｜');

    console.log('跳转购物车=====>',strS);
    app.globalData.community_id_for_switch_tab = strS;
    wx.switchTab({
      url: '/pages/shopping_cart/shopping_cart'
    })
  },
  //取消订单
  go_order_cancel(e){
    var that = this;
    let order_id = e.currentTarget.dataset.order_id;
    var url = 'index.php?act=member_order&op=order_cancel';
    mph.postRequest(url, { order_id: order_id }, function (res) {

      if (res.data.code == 200) {
        wx.redirectTo({
          url: '/pages/my_order/my_order?name=state_new'
        })
      }
    })
  },
  /***
   * 确认收货
   */
  go_order_receive:function(e){
    var that = this;
    let order_id = e.currentTarget.dataset.order_id;
    var url = 'index.php?act=member_order&op=order_receive';
    mph.postRequest(url, { order_id: order_id }, function (res) {
     
      if(res.data.code==200){
              wx.redirectTo({
                  url: '/pages/my_order/my_order?name=state_noeval'
                })
      }
    })
  },

  // 滚动切换标签样式
  switchTab: function (e) {
    this.setData({
      currentTab: e.detail.current
    });
    this.checkCor();
  },
  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
    var cur = e.target.dataset.current;
    if (this.data.currentTaB == cur) { return false; }
    else {
     this.setData({
        currentTab: cur
      })
     var val = this.data.expertList[cur]
       this.add_order_list(val.u_name);
    }
  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function () {
    if (this.data.currentTab > 4) {
      this.setData({
        scrollLeft: 300
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },
  add_order_list(state) {
    var that = this;
    let k = wx.getStorageSync('key');
    var url = 'index.php?act=member_order&op=order_list';
      mph.postRequest(url, {state_type:state},function(res){
        
        if(res.data.code==200){
          console.log('我的订单列表:',res.data.datas);
          that.setData({
            contentList:res.data.datas.order_group_list
          }) 
        }
    });

  },
  onLoad: function (option) {
    let u_name = option.name;//我的订单里面的u_name
     let that = this;
    if(u_name=='')u_name='';
    that.add_order_list(u_name);

    //跳转到我的订单对应的栏目中
    for (let i = 0; i < that.data.expertList.length;i++){
      if (that.data.expertList[i].u_name == u_name){
        let cur = i;

        if (this.data.currentTaB == cur) { return false; }
        else {
          this.setData({
            currentTab: cur
          })
        }
      }
    }
    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 180;
//console.log(calc)
        that.setData({
          winHeight: calc
        });
      }
    });
  },
  footerTap: app.footerTap,
  /**
 * 生命周期函数--监听页面初次渲染完成
 */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
